package ar.com.notarip.bb.infosubte;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;

public class ImageUtil {
	
	
	public static Bitmap getScaledBitmapImage(String imagename, int ratioX, int ratioY)
    {
        
        EncodedImage image = EncodedImage.getEncodedImageResource(imagename); 
        
        int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
         
        double ratio = (double)ratioX / (double) ratioY;
       double w = (double) image.getWidth() * ratio; 
       double h = (double)image.getHeight() * ratio;
       int width = (int) w;
       int height = (int) h;
                
        int requiredWidthFixed32 = Fixed32.toFP(width);
        int requiredHeightFixed32 = Fixed32.toFP(height);
        
        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
        
        image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
        
        return image.getBitmap();
    }
	
	
	public static  EncodedImage sizeImage(EncodedImage image, int width, int height) {
		
		  EncodedImage result = null;
		
		  int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
		  int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
		
		  int requiredWidthFixed32 = Fixed32.toFP(width);
		  int requiredHeightFixed32 = Fixed32.toFP(height);
		
		  int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
		    requiredWidthFixed32);
		  int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
		    requiredHeightFixed32);
		
		  result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
		  return result;
		  
	}

}
